package com.sinodata.bsm.cicp.collector.vsphere;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.finder.vsphere.EsxOS;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.cicp.util.SnmpUtil;
import com.sinodata.bsm.cicp.util.ValueBuffer;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.utils.MD5Encoder;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class VMCollector extends Collector {
    private static Logger logger = Logger.getLogger(VMCollector.class);
    private static final String STAT_OID = ".1.3.6.1.4.1.6876.2.1.1.6";
    private static final String CPU_OID = ".1.3.6.1.4.1.6876.2.1.1.9";
    private static final String MEME_OID = ".1.3.6.1.4.1.6876.2.1.1.5";
    private static final String VM_CONFIG_OID = ".1.3.6.1.4.1.6876.2.1.1.3";
    private static final String VM_ID_OID = ".1.3.6.1.4.1.6876.2.1.1.7";
    // .1.3.6.1.4.1.6876.2.4.1.3.128.4000
    private static final String NET_NAME_OID = ".1.3.6.1.4.1.6876.2.4.1.3";
    // .1.3.6.1.4.1.6876.2.4.1.6.48.4000
    private static final String NET_CONNECT_OID = ".1.3.6.1.4.1.6876.2.4.1.6";

    private static final String NET_MAC_OID = ".1.3.6.1.4.1.6876.2.4.1.7";
    static String col = SeparatorConstants.COL_SEPARATOR;

    static String lin = SeparatorConstants.LIN_SEPARATOR;
    private static final int TIME_OUT = 30 * 60 * 1000;

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> results = new ArrayList<ResPropertyValue>();
        String uuid = this.paramMap.get("VMID");
        //String vmId =
        /////////////////////////////////////////////////////////

        String vmId = (String) ValueBuffer.getInstance().get(ip + "_" + uuid, TIME_OUT);
        String values = null;
        if (vmId == null) {
            try {
                values = SnmpUtil.getValueBySnmpTable(ip, VM_ID_OID + "," + VM_CONFIG_OID, "public");
            } catch (Exception e1) {
                values = "";
            }
            logger.info("get VM_CONFIG_OID ");
            String[] rows = values.split("\n");
            for (int i = 0; i < rows.length; i++) {
                String[] cols = rows[i].split("#");
                String vmid = MD5Encoder.encode(cols[1]);
                ValueBuffer.getInstance().put(ip + "_" + vmid, cols[0]);
            }
            vmId = (String) ValueBuffer.getInstance().get(ip + "_" + uuid, TIME_OUT);
        }

        ////////////////////////////////////////////////////////
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        String[] oids = new String[] { STAT_OID + "." + vmId, CPU_OID + "." + vmId, MEME_OID + "." + vmId };
        try {
            Map<Integer, String> map = SnmpUtil.getValueBySnmp(ip, oids, "public");
            for (int i = 0; i < 3; i++) {

                propValues[i] = ResPropertyValue.createPropValue(map.get(i), propMetas[i]);
            }
        } catch (Exception e) {
            logger.error("error collect VM,", e);
        }
        String toids = NET_NAME_OID + "." + vmId + "," + NET_CONNECT_OID + "." + vmId + "," + NET_MAC_OID + "." + vmId;
        StringBuffer bf = new StringBuffer();
        try {
            String tables = SnmpUtil.getValueBySnmpTable2(ip, toids, "public");

            String[] array = tables.split("[\n]");
            for (int i = 0; i < array.length; i++) {
                String[] cols = array[i].split("[#]");
                for (int j = 0; j < cols.length; j++) {
                    bf.append(cols[j]);
                    bf.append(SeparatorConstants.COL_SEPARATOR);
                }
                bf.append(SeparatorConstants.LIN_SEPARATOR);
            }

        } catch (Exception e) {
            logger.error("error collect table data, oids:" + toids, e);
        }
        propValues[propMetas.length - 1] = ResPropertyValue.createPropValue(bf.toString(), propMetas[propMetas.length - 1]);
        propertyValueX.setValues(propValues);
        results.add(propertyValueX);
        return results;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "VMID" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1", "$2", "$3", "$4" };

    }

}
